package com.ebelter.bodyfatscale.ui.pager.main;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ebelter.bodyfatscale.R;
import com.ebelter.bodyfatscale.common.AppDateLitConstants;
import com.ebelter.bodyfatscale.common.Constants;
import com.ebelter.bodyfatscale.moudules.https.NetUtils;
import com.ebelter.bodyfatscale.moudules.https.interfaces.HttpResponse;
import com.ebelter.bodyfatscale.moudules.https.response.HttpsParameters;
import com.ebelter.bodyfatscale.moudules.https.response.OriginalParametersResponse;
import com.ebelter.bodyfatscale.ui.activity.MainActivity;
import com.ebelter.bodyfatscale.util.AppUtils;
import com.ebelter.bodyfatscale.util.NumUtils;
import com.ebelter.bodyfatscale.util.ScaleBaseUtils;
import com.ebelter.bodyfatscale.util.StringUtils;
import com.ebelter.bodyfatscale.util.ToastUtil;
import com.ebelter.bodyfatscale.util.UserSpUtil;
import com.ebelter.bodyfatscale.util.ViewUtils;
import com.ebelter.btlibrary.util.ULog;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 未绑定提示页
 */

public class GoalPager extends BasePagerMain {

    private static final String TAG = "GoalPager";

    @BindView(R.id.pager_goal_currentWeight_tv)
    TextView pager_goal_currentWeight_tv;

    @BindView(R.id.pager_goal_suggtion1_tv)
    TextView pager_goal_suggtion1_tv;

    @BindView(R.id.pager_goal_targetWeight_ed)
    EditText pager_goal_targetWeight_ed;

    @BindView(R.id.pager_goal_targetWeight_unit_tv1)
    TextView pager_goal_targetWeight_unit_tv1;

    @BindView(R.id.pager_goal_targetWeight_unit_tv2)
    TextView pager_goal_targetWeight_unit_tv2;

    @BindView(R.id.pager_goal_targetWeight_tip_tv)
    TextView pager_goal_targetWeight_tip_tv;

    @BindView(R.id.pager_goal_suggtion2_tv)
    TextView pager_goal_suggtion2_tv;


    @BindView(R.id.pager_goal_targetWeight_set_tv)
    TextView pager_goal_targetWeight_set_tv;


    private float currentWeight;
    private float targetWeight;//kg


    private float idealWeight_min;
    private float idealWeight_max;


    private String unitKg = StringUtils.getResStr(R.string.kg);
    private String unitLb = StringUtils.getResStr(R.string.lb);


    public GoalPager(Activity context) {
        super(context);
    }

    @Override
    protected int loadLayoutById() {
        return R.layout.pager_goal;
    }

    int height;
    //初始化数据
    public void initBaseData(int height){

        this.height=height;
        setIdeaWeightTip(height);//根据身高推算建议体重
        updateUnitText();
        this.targetWeight= UserSpUtil.readFloat(Constants.IUser.TARGET_WEIGHT);//这个体重是服务器的 单位永远是kg
        this.currentWeight= UserSpUtil.readFloat(Constants.IUser.WEIGHT);//这个体重是服务器的 单位永远是kg
        displayTargetWeight();
        setCurrentWeight(false,currentWeight);
    }

    @SuppressLint("SetTextI18n")
    private void displayTargetWeight() {
        if (pager_goal_targetWeight_tip_tv!=null){
            ULog.i(TAG,"---A--displayTargetWeight---targetWeight="+targetWeight);
            if (targetWeight<=0){
                targetWeight = (float) ((idealWeight_max+idealWeight_min)/2.0);
                if (!Constants.isKgUnit){
                    targetWeight=ScaleBaseUtils.lb2kg(targetWeight);
                }
            }
            ULog.i(TAG,"---B--displayTargetWeight---targetWeight="+targetWeight);
//            targetWeight=NumUtils.numBaoLiuWei(targetWeight,1);
//            pager_goal_targetWeight_ed.setText(ScaleBaseUtils.adapterDataFloat(targetWeight)+"");

            String targetWeightStr=NumUtils.numBaoLiuWei(ScaleBaseUtils.adapterDataFloat(targetWeight),1)+"";
            ULog.i(TAG,"---C--displayTargetWeight---targetWeightStr="+targetWeightStr);

            pager_goal_targetWeight_ed.setText(targetWeightStr);
        }
    }

    //更新目标单位
    private void updateUnitText(){
        String local = AppUtils.getLocalStr();
        if (TextUtils.equals(AppUtils.iw,local)){
            if (pager_goal_targetWeight_unit_tv1!=null){
                ViewUtils.displayView(pager_goal_targetWeight_unit_tv2);
                pager_goal_targetWeight_unit_tv1.setText(Constants.isKgUnit?unitKg:unitLb);
                ViewUtils.goneView(pager_goal_targetWeight_unit_tv2);
            }
        }else {
            if (pager_goal_targetWeight_unit_tv2!=null){
                ViewUtils.displayView(pager_goal_targetWeight_unit_tv1);
                pager_goal_targetWeight_unit_tv2.setText(Constants.isKgUnit?unitKg:unitLb);
                ViewUtils.goneView(pager_goal_targetWeight_unit_tv1);
            }
        }

    }


    private void setIdeaWeightTip(int height){
        if (pager_goal_suggtion1_tv!=null){
            pager_goal_suggtion1_tv.setText(getIdeaWeightTip(height));
        }
    }

    //根据获取体重建议值文本
    private String getIdeaWeightTip(int height) {

        float min = ScaleBaseUtils.getIdeaWeightMin(height);
        float max = ScaleBaseUtils.getIdeaWeightMax(height);

        min = NumUtils.numBaoLiuWei(min,1);
        max = NumUtils.numBaoLiuWei(max,1);

        String ideaWeight= ScaleBaseUtils.getStr(R.string.Your_ideal_weight_1_1);

        if (Constants.isKgUnit){
            idealWeight_min=min;
            idealWeight_max=max;
            //String kgUnit=StringUtils.getResStr(R.string.kg);

           // return String.format(ideaWeight,ScaleBaseUtils.unitDisplay(min,kgUnit),ScaleBaseUtils.unitDisplay(max,kgUnit));
            return ScaleBaseUtils.placeHolder2(ideaWeight,ScaleBaseUtils.unitDisplay(NumUtils.numBaoLiuWei(min,1),unitKg),ScaleBaseUtils.unitDisplay(NumUtils.numBaoLiuWei(max,1),unitKg));
        }else {
            min=ScaleBaseUtils.kg2lb(min);
            max=ScaleBaseUtils.kg2lb(max);
            idealWeight_min=min;
            idealWeight_max=max;
           // String lbUnit = StringUtils.getResStr(R.string.lb);
            //return String.format(ideaWeight,min,"lb",max,"lb");
            return ScaleBaseUtils.placeHolder2(ideaWeight,ScaleBaseUtils.unitDisplay(NumUtils.numBaoLiuWei(min,1),unitLb),ScaleBaseUtils.unitDisplay(NumUtils.numBaoLiuWei(max,1),unitLb));
        }
    }




    @OnClick({ R.id.pager_goal_targetWeight_set_tv,R.id.pager_goal_targetWeight_set_tv2})
    public void btClick(View view){
        switch (view.getId()){
            case R.id.pager_goal_targetWeight_set_tv://set
                settingTargetWeight();
                break;
            case R.id.pager_goal_targetWeight_set_tv2://set
                setCurrentWeight(true,new Random().nextInt(100));
                break;


        }
    }
    //设置目标体重
    private void settingTargetWeight() {
        ULog.i(TAG,"------settingTargetWeight()");
        float target = NumUtils.string2Float(pager_goal_targetWeight_ed.getText().toString().trim());
       // target = NumUtils.numBaoLiuWei(target,1);

        if (Constants.isKgUnit){
            if (target>= AppDateLitConstants.KG_MIN&&target<AppDateLitConstants.KG_MAX){
                saveTargetWeight(target,true);
            }else {
                ToastUtil.show(String.format(ScaleBaseUtils.getStr(R.string.Please_enter_a_reasonable_weight_1_1),AppDateLitConstants.KG_MIN,AppDateLitConstants.KG_MAX));
            }
        }else {
            if (target>= AppDateLitConstants.LB_MIN&&target<AppDateLitConstants.LB_MAX){
                saveTargetWeight(target,false);
            }else {
                ToastUtil.show(String.format(ScaleBaseUtils.getStr(R.string.Please_enter_a_reasonable_weight_1_1),AppDateLitConstants.LB_MIN,AppDateLitConstants.LB_MAX));
            }
        }

    }

    //保存目标体重
    private void saveTargetWeight(float target, boolean isKg) {
        float targetWeight2;
        if(isKg){
            targetWeight2=target;
        }else {
            targetWeight2=ScaleBaseUtils.lb2kg(target);
        }
      //  targetWeight2=NumUtils.numBaoLiuWei(targetWeight2,1);//就是服务器上存的永远是千克为单位

        //targetWeight永远以kg的单位上传到服务器

        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }

        Map<String,Serializable> paramMap=new HashMap<>();
        paramMap.put(HttpsParameters.USER_ID, UserSpUtil.readLong(Constants.IUser.USER_ID));
        paramMap.put(HttpsParameters.TARGET_WEIGHT,targetWeight2);
        final float finalTargetWeight = targetWeight2;
        NetUtils.getInstance().setTargetWeight(this, paramMap, new HttpResponse<OriginalParametersResponse>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, OriginalParametersResponse originalParametersResponse) {
                if (isRequestSuccess){

                    if (originalParametersResponse.resultCode==0){
                        UserSpUtil.writeFloat(Constants.IUser.TARGET_WEIGHT, finalTargetWeight);
                        targetWeight = finalTargetWeight;
                        setCurrentWeight(false,currentWeight);//更新一下界面的意思
                        //更新TrendsPager
                        if(getActivity()!=null){
                            ((MainActivity)getActivity()).trendPagerUpdateTargetWeight();
                        }
                        ToastUtil.show(R.string.Set_success);

                    }else if (originalParametersResponse.resultCode==110){
                        ToastUtil.show(R.string.An_exception_occurred_on_the_server);
                    } else {
                        ToastUtil.show(R.string.Modify_Failed);
                    }
                }else {
                    ToastUtil.show(R.string.Network_connection_error);
                }
            }
        });





    }





    //设置当前体重显示值
    @SuppressLint("SetTextI18n")
    public void setCurrentWeight(boolean comefromDevice,float currentWeight){

        if (!comefromDevice){
            if (currentWeight<=0){//如果当前体重小于零时把两个控件隐藏掉
                ViewUtils.goneView(pager_goal_targetWeight_tip_tv);
                ViewUtils.goneView(pager_goal_suggtion2_tv);
                return;
            }
        }
        if (currentWeight<0){
            return;
        }
        ViewUtils.displayView(pager_goal_targetWeight_tip_tv);//带背景的按钮提示
        ViewUtils.displayView(pager_goal_suggtion2_tv);//提示文字

        String unit=Constants.isKgUnit? unitKg:unitLb;
        if (pager_goal_currentWeight_tv!=null){
            String cw=NumUtils.numBaoLiuWei(ScaleBaseUtils.adapterDataFloat(currentWeight),1)+"";
            if (TextUtils.equals(unit,unitLb)){
                cw=ScaleBaseUtils.unitDisplay2(cw,unit);
            }else {
                cw=ScaleBaseUtils.unitDisplay(cw,unit);
            }

            pager_goal_currentWeight_tv.setText(cw);
        }

        float c=NumUtils.numBaoLiuWei(ScaleBaseUtils.adapterDataFloat(currentWeight),1);
        float t=NumUtils.numBaoLiuWei(ScaleBaseUtils.adapterDataFloat(targetWeight),1);
        float differenceValue=Math.abs(c-t);

        if (c==t){
            pager_goal_targetWeight_tip_tv.setText(R.string.str_Maintain);
            pager_goal_suggtion2_tv.setText(R.string.Youre_doing_great_keep_it_up);
        }else if (c>t){
            pager_goal_targetWeight_tip_tv.setText(R.string.str_Lose);
            pager_goal_suggtion2_tv.setText(String.format(ScaleBaseUtils.getStr(R.string.Lose_1_to_achieve_this_goal),differenceValue,unit));
        }else {
           // differenceValue=NumUtils.numBaoLiuWei(differenceValue,1);
            pager_goal_targetWeight_tip_tv.setText(R.string.str_Gain);
            pager_goal_suggtion2_tv.setText(String.format(ScaleBaseUtils.getStr(R.string.Gain_1_1f_to_achieve_this_goal),differenceValue,unit));
        }


    }




    @Override
    public void initViews() {

    }

    @Override
    public void initData(Object data) {

    }

    @Override
    public void switchUnitUpdate() {
        initBaseData(this.height);
    }
}
